package com.qian2.pricesharingapplication;

import com.example.lb2.JSONParser;
import com.example.pricesharingapplication.R;
import com.qian2.pricesharingapplication.shoppinglist.ShoppingList;

import android.accounts.AccountManager;
import android.app.ActionBar;
import android.app.Activity;
import android.app.TabActivity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.inputmethod.InputMethodManager;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TabHost.TabSpec;
import android.widget.TextView;

public class MainActivity extends TabActivity {
	Menu absTopSubMenus = null;
	int temp_menu = -1;
	public static Activity all_product_activity = null;
	public ShoppingList shopping_list_activity = null;
	TabHost tabHost = null;
	private Language myLanguage;
	public static final String MyPREFERENCES = "MyPrefs";
	public static final String Email = "emailKey";
	static SharedPreferences sharedpreferences;

	JSONParser jsonParser = new JSONParser();
	private static Activity Search_activity;
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		ActionBar bar = getActionBar();
		bar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#77CEDA"))); 
		int titleId = Resources.getSystem().getIdentifier("action_bar_title", "id", "android");
		TextView yourTextView = (TextView)findViewById(titleId);
		yourTextView.setTextColor(Color.parseColor("#005d83"));
		getActionBar().setIcon(R.drawable.news_icon);
		AccountManager.get(this);
		new AuthPreferences(this);

		sharedpreferences = getSharedPreferences(MyPREFERENCES,
				Context.MODE_PRIVATE);
		
		// TODO initial language class change language
		myLanguage = new Language(sharedpreferences, getBaseContext() );
		myLanguage.loadLocale();
		setContentView(R.layout.main);
		tabHost = getTabHost();
		Resources ressources = getResources();

		// Scanner tab
		Intent intentScanner = new Intent().setClass(this,
				barcode_Scanner.class);
		TabSpec tabScanner = tabHost
				.newTabSpec("Scanner")
				.setIndicator("Scanner")
				.setIndicator(
						"",
						ressources
								.getDrawable(R.drawable.ic_tab_barcode_scanner))
				.setContent(intentScanner);

		// Suggested tab
		Intent intentAll_products = new Intent().setClass(this,
				AllProductsActivity.class);
		TabSpec tabAll_products = tabHost
				.newTabSpec("Suggested Product")
				.setIndicator("Suggested Product")
				.setIndicator("",
						ressources.getDrawable(R.drawable.ic_tab_star))
				.setContent(intentAll_products);

		//ShoppingList tab
		Intent intentShoppinglist = new Intent().setClass(this,
				ShoppingList.class);
		TabSpec tabShoppinglist = tabHost
				.newTabSpec("Shoppinglist")
				.setIndicator("Shoppinglist")
				.setIndicator("",
						ressources.getDrawable(R.drawable.ic_tab_shopping_list))
				.setContent(intentShoppinglist);

		//Search tab
		Intent intentSearch = new Intent().setClass(this, SearchProduct.class);
		TabSpec tabSearch = tabHost
				.newTabSpec("Search")
				.setIndicator("Search")
				.setIndicator("",
						ressources.getDrawable(R.drawable.ic_tab_search))
				.setContent(intentSearch);

		//Create Product tab
		Intent intentCreate = new Intent().setClass(this,
				NewProductActivity.class);
		TabSpec tabCreate = tabHost
				.newTabSpec("Create Product")
				.setIndicator("Create Product")
				.setIndicator("",
						ressources.getDrawable(R.drawable.ic_tab_plus))
				.setContent(intentCreate);
		
		//History tab
		Intent intentHistory = new Intent().setClass(this,
				History.class);
		
		TabSpec tabHistory = tabHost
				.newTabSpec("History")
				.setIndicator("History")
				.setIndicator("",
						ressources.getDrawable(R.drawable.tab_calendar))
				.setContent(intentHistory);


		// add all tabs
		tabHost.addTab(tabScanner);
		tabHost.addTab(tabAll_products);
		tabHost.addTab(tabSearch);
		tabHost.addTab(tabCreate);
		tabHost.addTab(tabShoppinglist);
		tabHost.addTab(tabHistory);

		
		tabHost.setCurrentTab(0);
		tabHost.setOnTabChangedListener(new OnTabChangeListener() {

			@Override
			public void onTabChanged(String tabId) {
				// TODO Auto-generated method stub
				if (tabId.equals("Test")) {
					absTopSubMenus.clear();
					temp_menu = R.menu.main;
					onCreateOptionsMenu(absTopSubMenus);
				} else if (tabId.equals("Search")) {
					absTopSubMenus.clear();
					temp_menu = R.menu.main;
					onCreateOptionsMenu(absTopSubMenus);
				} else if (tabId.equals("Suggested Product")) {
					absTopSubMenus.clear();
					temp_menu = R.menu.options_allproduct;
					onCreateOptionsMenu(absTopSubMenus);
				} else if (tabId.equals("Shoppinglist")) {
					absTopSubMenus.clear();
					temp_menu = R.menu.options_shopping_list;
					onCreateOptionsMenu(absTopSubMenus);
					if (shopping_list_activity!=null){
						shopping_list_activity.refresh();
					}
					
				} else {
					absTopSubMenus.clear();
					temp_menu = R.menu.main;
					onCreateOptionsMenu(absTopSubMenus);
				}

				InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
				imm.hideSoftInputFromWindow(
						tabHost.getApplicationWindowToken(), 0);
			}
		});

	}
	public TabHost return_tab(){
		return tabHost;
	}
	

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.refresh:
			((AllProductsActivity) all_product_activity).creatlist();
			return true;
		case R.id.add_item:
			((ShoppingList) shopping_list_activity).add_item();
			return true;
		case R.id.langauge:
			System.out.println("MainActivity.onOptionsItemSelected():"+ myLanguage.getLanguage());
			if (myLanguage.getLanguage().equals("zh")) {
				myLanguage.changeLang("en");
			} else if(myLanguage.getLanguage().equals("en")){
				myLanguage.changeLang("zh");
			}
			 startActivity(getIntent()); finish();
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		if (absTopSubMenus == null)
			absTopSubMenus = menu;
		if (temp_menu != -1) {
			
			MenuInflater inflater = getMenuInflater();
			inflater.inflate(temp_menu, menu);
		}

		return super.onCreateOptionsMenu(menu);
	}

	public void temp(Activity activity) {
		if (tabHost.getCurrentTabTag() == "Suggested Product")
			all_product_activity = activity;
		else if (tabHost.getCurrentTabTag() == "Shoppinglist")
			shopping_list_activity = (ShoppingList) activity;
		else if (tabHost.getCurrentTabTag() == "Search")
			setSearch_activity(activity);
	}
	public Activity getSearch_activity() {
		return Search_activity;
	}
	public static void setSearch_activity(Activity search_activity) {
		Search_activity = search_activity;
	}

}
